package com.nidu.demo.dict.ability;

import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.dict.gateway.DictTypeGateway;
import com.nidu.demo.dict.model.DictType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class DictTypeDomainService {

    private final DictTypeGateway gateway;

    public void validateDuplicate(String type, Long excludeId){
        DictType dictType = gateway.getByType(type);
        if(dictType != null){
            if(excludeId == null){
                throw new BizException(ErrorCodeConstants.DICT_TYPE_TYPE_DUPLICATE.getCode(),
                        ErrorCodeConstants.DICT_TYPE_TYPE_DUPLICATE.getMessage());
            } else if (!excludeId.equals(dictType.getId())) {
                throw new BizException(ErrorCodeConstants.DICT_TYPE_TYPE_DUPLICATE.getCode(),
                        ErrorCodeConstants.DICT_TYPE_TYPE_DUPLICATE.getMessage());
            }
        }
    }

    public void validateDictType(String type) {
        if(!StrUtil.isEmpty(type)){
            DictType dictType = gateway.getByType(type);
            if (dictType == null) {
                throw new BizException(ErrorCodeConstants.DICT_TYPE_NOT_FOUND.getCode(),
                        ErrorCodeConstants.DICT_TYPE_NOT_FOUND.getMessage());
            }
            dictType.isDisable();
            dictType.isDeleted();
        }
    }
}
